package com.stockz.equity.scanner.screen;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.stockz.equity.indicator.movingAverage.simple.SimpleMovingAverageIndicator;
import com.stockz.equity.model.Bar;
import com.stockz.equity.model.Bar.BarValueType;
import com.stockz.equity.scanner.screen.configuration.ConsolidationScreenConfiguration;
import com.stockz.equity.util.MathUtil;

@Component
public class ConsolidationScreen implements Screen<ConsolidationScreenConfiguration> {

	@Autowired
	private SimpleMovingAverageIndicator simpleMovingAverageIndicator;
	
	private ConsolidationScreenConfiguration configuration = new ConsolidationScreenConfiguration();
	
	@Override
	public ConsolidationScreenConfiguration getConfiguration() {
		return configuration;
	}

	@Override
	public int getMandatorySpan() {
		return configuration.getSmaSpan()+configuration.getBoundSpan();
	}

	@Override
	public boolean isMatch(List<Bar> barList) {
		simpleMovingAverageIndicator.setBarValueType(BarValueType.AVGPRICE);
		simpleMovingAverageIndicator.getIndicatorConfiguration().setSpan(configuration.getSmaSpan());
		double[] smaValues = simpleMovingAverageIndicator.getValues(barList.subList(0, getMandatorySpan()));
		boolean isMatch = true;
		double lowestSmaValue = MathUtil.min(smaValues);
		double upperLimit = lowestSmaValue + lowestSmaValue*configuration.getBoundPercentage()/100;
		for(double smaValue : smaValues){
			if(smaValue > upperLimit){
				isMatch = false;
				break;
			}
		}
		return isMatch;
	}

}
